package cn.com.hhrcw.utils;

import cn.com.hhrcw.entity.*;
import cn.com.hhrcw.service.*;
import cn.com.hhrcw.sms.service.ISysSmsTemplateService;
import cn.com.hhrcw.wx.util.WxTemplateUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lixingxing
 */
@Component
@Slf4j
public class NewsUtil {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysSmsTemplateService sysSmsTemplateService;

    @Autowired
    private IPiInfoService piInfoService;

    @Autowired
    private IOpDeliveryHistoryService opDeliveryHistoryService;

    @Autowired
    private IOpJobService opJobService;

    @Autowired
    private IOpInterviewHistoryService opInterviewHistoryService;

    @Autowired
    private IEnInfoService enInfoService;

    @Autowired
    private IOpEntryHistoryService opEntryHistoryService;

    @Autowired
    private IEnNewsService enNewsService;

    @Autowired
    private WxTemplateUtil wxTemplateUtil;

    /**
     * @param receiveId   接收者id
     * @param sendId      发送者id
     * @param receiveType 接收者类型 0个人1企业
     * @param sendType    发送者类型 0个人1企业
     * @param objId       对应内容id
     * @param newsType    消息内容类型 类型0投递1面试2录用3拒绝面试4接受面试5阅读6接受入职7拒绝入职8简历不合适通知
     *                    9面试通过10面试未通过11面试时间被修改12面试即将开始通知
     * @return
     */
    public Boolean creatNews(String receiveId, String sendId, Integer receiveType, Integer sendType, String objId, String newsType) {
        PiInfo userState = piInfoService.lambdaQuery().eq(PiInfo::getUserId, sendId).eq(PiInfo::getDelFlag, false).one();
        PiInfo receiveUser = piInfoService.lambdaQuery().eq(PiInfo::getUserId, receiveId).eq(PiInfo::getDelFlag, false).one();
        boolean isMan = false;
        boolean isWoMan = false;
        if (receiveUser.getSex() != null) {
            isMan = receiveUser.getSex() == 1 || receiveUser.getSex() == 0;
            isWoMan = receiveUser.getSex() == 2;
        }
        String phone = userState.getPhone();
        userState.setPhone(phone.replace("@ent", "").replace("@admin", ""));
        String content = "";
        String tittle = "";
        //Sim
        Map<String, Object> map = new HashMap<>(5);
        map.put("name", userState.getName());
        map.put("phone", userState.getPhone());
        if (isMan) {
            map.put("sex", "先生");
        } else if (isWoMan) {
            map.put("sex", "女士");
        }
        int typeInt = Integer.parseInt(newsType);
        switch (typeInt) {
            case 0:
                this.deliveryNotice(map, objId);
                content = sysSmsTemplateService.sendNews("delivery_resume", map);
                tittle = sysSmsTemplateService.getTittle("delivery_resume");
                break;
            case 1:
                map.put("name", receiveUser.getName());
                OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
                this.onlineInterviewNotic(map, objId);
                String code = interviewHistory.getType() == 1 ? "inview_notice_up" : "inview_notice_down";
                content = sysSmsTemplateService.sendNews(code, map);
                tittle = sysSmsTemplateService.getTittle(code);
                break;
            case 2:
                map.put("name", receiveUser.getName());
                this.entryInvitation(map, objId);
                content = sysSmsTemplateService.sendNews("entry_notice", map);
                tittle = sysSmsTemplateService.getTittle("entry_notice");
                break;
            case 3:
                this.interviewAcceptAndRejct(map, objId);
                content = sysSmsTemplateService.sendNews("inview_refuse_notice", map);
                tittle = sysSmsTemplateService.getTittle("inview_refuse_notice");
                break;
            case 4:
                this.interviewAcceptAndRejct(map, objId);
                content = sysSmsTemplateService.sendNews("inview_accept_notice", map);
                tittle = sysSmsTemplateService.getTittle("inview_accept_notice");
                break;
            case 5:
                map.put("name", receiveUser.getName());
                this.read(map, objId);
                content = sysSmsTemplateService.sendNews("read_notice", map);
                tittle = sysSmsTemplateService.getTittle("read_notice");
                break;
            case 6:
                this.acceptEntry(map, objId);
                content = sysSmsTemplateService.sendNews("entry_accept_notice", map);
                tittle = sysSmsTemplateService.getTittle("entry_accept_notice");
                break;
            case 7:
                this.acceptEntry(map, objId);
                content = sysSmsTemplateService.sendNews("entry_refuse_notice", map);
                tittle = sysSmsTemplateService.getTittle("entry_refuse_notice");
                break;
            case 8:
                this.reason(map, objId);
                content = sysSmsTemplateService.sendNews("resume_inappropriate", map);
                tittle = sysSmsTemplateService.getTittle("resume_inappropriate");
                break;
            case 9:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("inview_notice_pass", map);
                tittle = sysSmsTemplateService.getTittle("inview_notice_pass");
                break;
            case 10:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("inview_notice_no_pass", map);
                tittle = sysSmsTemplateService.getTittle("inview_notice_no_pass");
                break;
            case 11:
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("update_time_notice_msg", map);
                tittle = sysSmsTemplateService.getTittle("update_time_notice_msg");
                break;
            case 12:
                map.put("phone", receiveUser.getPhone());
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("interview_begin_notice_msg", map);
                tittle = sysSmsTemplateService.getTittle("interview_begin_notice_msg");
                break;
            case 13:
                map.put("phone", receiveUser.getPhone());
                map.put("name", receiveUser.getName());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("interview_begin_cover_notice_msg", map);
                tittle = sysSmsTemplateService.getTittle("interview_begin_cover_notice_msg");
                break;
            case 14:
                map.put("phone", receiveUser.getPhone());
                this.interviewPass(map, objId);
                content = sysSmsTemplateService.sendNews("interview_start_notice_msg", map);
                tittle = sysSmsTemplateService.getTittle("interview_start_notice_msg");
                break;
            default:
                break;
        }
        return this.saveNews(sendId, newsType, objId, receiveId, receiveType, sendType, phone, userState.getName(), content, tittle);
    }

    /**
     * @param receiveId   接收者id
     * @param sendId      发送者id
     * @param receiveType 接收者类型 0个人1企业
     * @param sendType    发送者类型 0个人1企业
     * @param objId       对应内容id
     * @param newsType    消息内容类型 类型0投递1面试2录用3拒绝面试4接受面试5阅读6接受入职7拒绝入职8简历不合适通知
     *                    9面试通过10面试未通过11面试时间被修改12面试即将开始通知
     * @param name
     * @param phone
     * @param content
     * @param tittle
     * @return
     */
    boolean saveNews(String sendId, String newsType, String objId, String receiveId, Integer receiveType, Integer sendType, String phone, String name, String content, String tittle) {
        EnNews enNews = new EnNews();
        enNews.setDelFlag(false);
        enNews.setCreateBy(sendId);
        enNews.setCreateTime(DateTime.now());
        enNews.setUpdateTime(DateTime.now());
        enNews.setEnDelFlag(false);
        enNews.setNewsType(newsType);
        enNews.setReadFlag(0);
        enNews.setObjId(objId);
        enNews.setReceiveId(receiveId);
        enNews.setReceiveType(receiveType);
        enNews.setSendId(sendId);
        enNews.setSendType(sendType);
        enNews.setSendPhone(phone);
        enNews.setSendRealname(name);
        enNews.setNewsContent(content);
        enNews.setTitle(tittle);
        String uuid = IdUtil.fastSimpleUUID();
        enNews.setId(uuid);
        return enNewsService.save(enNews);
    }

    /**
     * 投递通知
     *
     * @param map
     * @param objId
     */
    void deliveryNotice(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String jobId = deliveryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("createTime", DateUtil.format(deliveryHistory.getCreateTime(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 在线面试通知 线下面试通知
     *
     * @param map
     * @param objId
     */
    void onlineInterviewNotic(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        map.put("time", DateUtil.format(interviewHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        String companyId = interviewHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
        map.put("address", enInfo.getEnLocation());
    }

    /**
     * 入职邀请
     *
     * @param map
     * @param objId
     */
    void entryInvitation(Map<String, Object> map, String objId) {
        OpEntryHistory entryHistory = opEntryHistoryService.getById(objId);
        map.put("time", DateUtil.format(entryHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
        String companyId = entryHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("address", enInfo.getEnLocation());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 面试邀请被拒绝 / 接受面试
     *
     * @param map
     * @param objId
     */
    void interviewAcceptAndRejct(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        String jobId = interviewHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("time", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 阅读
     *
     * @param map
     * @param objId
     */
    void read(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String companyId = deliveryHistory.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 接受入职
     *
     * @param map
     * @param objId
     */
    void acceptEntry(Map<String, Object> map, String objId) {
        OpEntryHistory entryHistory = opEntryHistoryService.getById(objId);
        String jobId = entryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
        map.put("time", DateUtil.format(new Date(), DatePattern.NORM_DATETIME_PATTERN));
        map.put("jobName", byId.getJobName());
    }

    /**
     * 面试结果
     *
     * @param map
     * @param objId
     */
    void reason(Map<String, Object> map, String objId) {
        OpDeliveryHistory deliveryHistory = opDeliveryHistoryService.getById(objId);
        String info = deliveryHistory.getInappropriateInfo();
        if (StrUtil.isNotBlank(info)) {
            switch (info) {
                case "0":
                    map.put("reason", "工作经验不满足要求");
                    break;
                case "1":
                    map.put("reason", "技术经验不符合岗位要求");
                    break;
                case "2":
                    map.put("reason", "月薪不匹配");
                    break;
                case "3":
                    map.put("reason", "学历不匹配");
                    break;
                case "4":
                    map.put("reason", "行业不匹配");
                    break;
                case "5":
                    map.put("reason", "招聘类型不匹配");
                    break;
                default:
                    map.put("reason", "无");
                    break;
            }
        }
        String jobId = deliveryHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("jobName", byId.getJobName());
        EnInfo enInfo = enInfoService.getById(byId.getCompanyId());
        map.put("enName", enInfo.getEnName());
    }

    /**
     * 面试通过通知
     *
     * @param map
     * @param objId
     */
    void interviewPass(Map<String, Object> map, String objId) {
        OpInterviewHistory interviewHistory = opInterviewHistoryService.getById(objId);
        String jobId = interviewHistory.getJobId();
        OpJob byId = opJobService.getById(jobId);
        map.put("jobName", byId.getJobName());
        String companyId = byId.getCompanyId();
        EnInfo enInfo = enInfoService.getById(companyId);
        map.put("enName", enInfo.getEnName());
        map.put("time", DateUtil.format(interviewHistory.getTime(), DatePattern.NORM_DATETIME_PATTERN));
    }
}
